package com.group2.view;

import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.net.Socket;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.UIManager;

//import com.group2.dao.LogOnDao;
import model.Student;
import com.group2.util.StringUtil;

import java.io.*;
import static model.netBase.IP;


import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import model.*;
/*用户管理模块：
 * 登录界面*/

public class UserLogOnFrame extends javax.swing.JFrame {

	private JPanel contentPane;
	private ButtonGroup buttonGroup1;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel_SEU;
	private JButton jb_logOn;
	private JLabel student;
	private JLabel admin;
	//private JButton jb_reset;
	private JButton jb_register;
	private JRadioButton jrb_admin;
	private JRadioButton jrb_student;
	private JPasswordField passwordTxt;
	private JFormattedTextField userNameTxt;

	public static Student currentStudent;

	public UserLogOnFrame() {

		Font font = new Font("Dialog", Font.PLAIN, 13);
		initComponents();
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		
		setBounds(100, 200, 500, 400);
		int wW=this.getWidth();
		int wH=this.getHeight();
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize=kit.getScreenSize();
		int sW=screenSize.width;
		int sH=screenSize.height;
		this.setLocation(sW/2-wW/2,sH/2-wH/2);
		this.jrb_student.setSelected(true);
	}

	private void initComponents() {
		contentPane = new JPanel();
		
		buttonGroup1 = new ButtonGroup();
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel_SEU=new JLabel();
		userNameTxt = new JFormattedTextField();
		jLabel3 = new JLabel();
		jrb_student = new JRadioButton("学生",true);
		jrb_admin = new JRadioButton("管理员");
		jb_logOn = new JButton();
		//jb_reset = new JButton();
		jb_register=new JButton();
		passwordTxt =new JPasswordField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("虚拟校园系统客户端登录 VCampus Log On！");
		setResizable(false);

		jLabel_SEU.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/SEU.png")));
		jLabel_SEU.setBounds(191,26,90,90);
		
		jLabel1.setFont(new Font("1", 1, 24));
		jLabel1.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/sys.png")));
		jLabel1.setText("Welcome to VCampus!");
		jLabel1.setBounds(89,114,321,50);

		jLabel2.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/userName.png")));
		jLabel2.setText("用户名");
		jLabel2.setBounds(111,169,70,30);

		jLabel3.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/password.png")));
		jLabel3.setText("密码");
		jLabel3.setBounds(111,195,62,30);

		buttonGroup1.add(jrb_student);
		student=new JLabel();
		student.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/student.png")));
		jrb_student.setBounds(191,227,62,39);
		student.setBounds(174,227,27,39);

		buttonGroup1.add(jrb_admin);
		admin=new JLabel();
		admin.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/admin.png")));
		jrb_admin.setBounds(274,227,70,39);
		admin.setBounds(254,231,27,35);

		jb_logOn.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/login.png")));
		jb_logOn.setBounds(165,272,83,30);
		jb_logOn.setText("登录");
		jb_logOn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				try {
					jb_logOnActionPerformed(evt);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		});
        
		
		jb_register.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/login.png")));
		jb_register.setText("注册");
		jb_register.setBounds(256,272,88,30);
		jb_register.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jb_RegisterActionPerformed(evt);
			}
		});
		
		passwordTxt.setBounds(191,200,153,21);
		userNameTxt.setBounds(191,174,153,21);

			contentPane.add(jLabel1);
			contentPane.add(jLabel2);
			contentPane.add(jLabel3);
			contentPane.add(jLabel_SEU);
			contentPane.add(passwordTxt);
			contentPane.add(userNameTxt);
			contentPane.add(jrb_student);
			contentPane.add(jrb_admin);
			contentPane.add(student);
			contentPane.add(admin);
			contentPane.add(jb_logOn);
			contentPane.add(jb_register);
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			contentPane.setLayout(null);
			setContentPane(contentPane);
	}

	//注册
	protected void jb_RegisterActionPerformed(ActionEvent evt) {
		this.dispose();
		new UserRegisterFrame().setVisible(true);
	}

	//登录
	private void jb_logOnActionPerformed(ActionEvent evt) throws IOException {

		String userName = userNameTxt.getText();
		String password = new String(passwordTxt.getPassword());

		if (StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(this, "用户名不能为空!");
			return;
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(this, "密码不能为空!");
			return;
		}
		try {
			Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
			//设置超时时间
			socket.setSoTimeout(3000);
			//构建IO
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			//传输用户名、密码和身份
			boolean role=this.jrb_student.isSelected();
			String s = String.valueOf(role);
			//向服务器传输用户身份（学生或管理员）、用户名、密码
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write("11"+'\n'+s+'\n'+userName + '\n' + password + '\n');
			bw.flush();
			//判断用户名密码是否正确
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			boolean flag=false;
			flag = Boolean.parseBoolean(br.readLine());
			System.out.println(flag);
			//flag-1 为学生 0-管理员
			if (role) {
				if (flag) {
					//进入学生VCampus主页面
					currentName=userName;
					this.dispose();
					new UserMainFrame_Student(currentName).setVisible(true);
				}
				else {
					JOptionPane.showMessageDialog(this, "学生不存在或密码错误!");
				}
			}
			else{
				if (flag) {
					//进入管理员VCampus主页面
					this.dispose();
					new UserMainFrame_Admin(currentName).setVisible(true);
				} else {
					JOptionPane.showMessageDialog(this, "管理员不存在或密码错误!");
				}
			}
			socket.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "连接异常!");
		}

	}

	private void jb_resetActionPerformed(ActionEvent evt) {
		this.resetValue();
	}

	//重置
	private void resetValue() {
		this.userNameTxt.setText("");
		this.passwordTxt.setText("");
		this.jrb_student.setSelected(true);
	}

	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new UserLogOnFrame().setVisible(true);
			}
		});
	}

    private String currentName;
}